package validate

import (
	"fmt"
	"github.com/go-playground/validator/v10"
	"go-shop-api/internal/service"
	"time"
)

// ValidatorValidation  "re_password": "re_password必须等于Password"       == >            "re_password": "re_password必须等于password"
func ValidatorValidation(sl validator.StructLevel) {
	su := sl.Current().Interface().(service.ValidatorService)
	//fmt.Println("this is in fun ValidatorValidation")
	//fmt.Println(su)
	//{10 zydh 11@qq.com 123456 1234567}
	if su.Password != su.RePassword {
		// 输出错误提示信息，最后一个参数就是传递的param
		sl.ReportError(su.RePassword, "re_password", "RePassword", "eqfield", "password")
	}
}

// UserStructValidation 自定义User结构体校验函数
//
//	"re_password": "re_password必须等于Password"       == >            "re_password": "re_password必须等于password"
func UserStructValidation(sl validator.StructLevel) {
	su := sl.Current().Interface().(service.UserService)

	if su.Password != su.RePassword {
		// 输出错误提示信息，最后一个参数就是传递的param
		sl.ReportError(su.RePassword, "re_password", "RePassword", "eqfield", "password")
	}
}

// customFunc 自定义字段级别校验方法
func customFunc(fl validator.FieldLevel) bool {
	date, err := time.Parse("2006-01-02", fl.Field().String())
	if err != nil {
		return false
	}
	if date.Before(time.Now()) {
		return false
	}
	return true
}

// validator v9以上写法
func bookableDate(fl validator.FieldLevel) bool {
	if date, ok := fl.Field().Interface().(time.Time); ok {
		today := time.Now()
		fmt.Println("date:", date)
		if date.Unix() > today.Unix() {
			fmt.Println("date unix ：", date.Unix())
			return true
		}
	}
	return false
}

func checkDate(fl validator.FieldLevel) bool {
	date, err := time.Parse("2006-01-02", fl.Field().String())
	if err != nil {
		return false
	}
	if date.Before(time.Now()) {
		return false
	}
	return true
}
